package com.wumeng;

import com.wumeng.service.PrinterDriver;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.util.*;

@Order(1)
@Component
public class ReflexApplicationRun implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<String> list = new ArrayList<>();
        list.add("xml");
        list.add("file");
        list.add("pdf");

        Properties properties = new Properties();

        properties.load(this.getClass().getResourceAsStream("/driver.properties"));

        String printerDriver = properties.getProperty("PrinterDriver");

        while (true) {
            int ceil = new Random().nextInt(printerDriver.split(";").length + 1);
            System.out.println(ceil);
            for (int i = 0; i < printerDriver.split(";").length; i++) {
                String split = printerDriver.split(";")[i];

                if (list.get(ceil).equals(split.split(",")[0])) {
                    Class<?> aClass = Class.forName(split.split(",")[1]);

                    PrinterDriver driver = (PrinterDriver) aClass.newInstance();

                    driver.print("hige");

                    Map<String, Object> obj = new HashMap<>();
                    String username = new String[]{"小明", "小红", "小刚"}[new Random().nextInt(3)];
                    obj.put("username", username);

                    driver.mapFun(obj);

                }
            }

            Thread.sleep(5000);
        }
    }
}
